package cn.shenlong.front.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.shenlong.portal.domain.Icon;
import cn.shenlong.portal.service.IconService;
import cn.shenlong.util.DirectiveUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component("cms_icon")
public class IconDirective implements TemplateDirectiveModel {
	@Autowired
	private IconService iconService;
	
	@SuppressWarnings({"rawtypes", "unchecked" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Long iconId = DirectiveUtils.getLong("iconId", params);
		if(iconId!=null){
			Icon icon = iconService.findIconByIconId(iconId);
			env.setVariable("icon", DEFAULT_WRAPPER.wrap(icon));
			body.render(env.getOut());
		}
	}
}
